﻿using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.BIZ;
namespace AnyOffice.general.work_plan
{
    public partial class read_workplan : ModulePage
    {
        public string GetStatusDesc(int status)
        {
            switch (status)
            {
                case 0:
                    return "<font color=\"red\"><b>未开始</font>";

                case 1:
                    return "<font color=\"blue\"><b>进行中</font>";

                case 2:
                    return "<font color=\"green\"><b>完成</font>";
            }
            return "";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            int planId = (Request.QueryString["plan_id"] != null) ? int.Parse(Request.QueryString["plan_id"]) : 0;
            DataTable table = new PlanWorkDao().SelectByPlanId(base.Conn, planId);
            if (table.Rows.Count == 0)
            {
                this.pnlMain.Visible = false;
                this.litMessage.Text = base.ShowPromptClose("无此项目");
            }
            else
            {
                ConfigManager manager = new ConfigManager();
                UserCache cache = new UserCache();
                bool flag = false;
                if (cache.PrivId == manager.AdministratorPrivId)
                {
                    flag = true;
                }
                if ((!flag && (table.Rows[0]["ToId"] != DBNull.Value)) && (table.Rows[0]["ToId"].ToString() == "ALL_DEPT"))
                {
                    flag = true;
                }
                if ((!flag && (table.Rows[0]["Participator"] != DBNull.Value)) && (string.Format(",{0},", table.Rows[0]["Participator"].ToString()).IndexOf(string.Format(",{0},", base.User.Identity.Name)) >= 0))
                {
                    flag = true;
                }
                if ((!flag && (table.Rows[0]["ToId"] != DBNull.Value)) && (string.Format(",{0},", table.Rows[0]["ToId"].ToString()).IndexOf(string.Format(",{0},", cache.DeptId.ToString())) >= 0))
                {
                    flag = true;
                }
                if (!flag && (table.Rows[0]["ToId"] != DBNull.Value))
                {
                    string str3 = string.Format(",{0},", table.Rows[0]["ToId"].ToString());
                    string postPriv = cache.PostPriv;
                    if (postPriv != null)
                    {
                        if (!(postPriv == "0"))
                        {
                            if (postPriv == "1")
                            {
                                flag = true;
                            }
                            else if (postPriv == "2")
                            {
                                if (str3.IndexOf(string.Format(",{0},", cache.DeptId.ToString())) >= 0)
                                {
                                    flag = true;
                                }
                                if (cache.PostDept.ToUpper() == "ALL_DEPT")
                                {
                                    flag = true;
                                }
                                else
                                {
                                    string postDept = cache.PostDept;
                                    if (postDept != string.Empty)
                                    {
                                        if (postDept.EndsWith(","))
                                        {
                                            postDept = postDept.Substring(0, postDept.Length - 1);
                                        }
                                        string[] strArray = postDept.Split(new char[] { ',' });
                                        if (strArray.Length > 0)
                                        {
                                            for (int i = 0; i < strArray.Length; i++)
                                            {
                                                if (str3.IndexOf(string.Format(",{0},", strArray[i])) >= 0)
                                                {
                                                    flag = true;
                                                    break;
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        else if (str3.IndexOf(string.Format(",{0},", cache.DeptId.ToString())) >= 0)
                        {
                            flag = true;
                        }
                    }
                }
                if ((!flag && (table.Rows[0]["Creator"] != DBNull.Value)) && (table.Rows[0]["Creator"].ToString() == base.User.Identity.Name))
                {
                    flag = true;
                }
                if ((!flag && (table.Rows[0]["Manager"] != DBNull.Value)) && (string.Format(",{0},", table.Rows[0]["Manager"].ToString()).IndexOf(string.Format(",{0},", base.User.Identity.Name)) >= 0))
                {
                    flag = true;
                }
                if (!flag)
                {
                    this.pnlMain.Visible = false;
                    this.litMessage.Text = base.ShowPromptClose("无权限查看");
                }
                else
                {
                    this.ViewState["Name"] = this.litNavTitle.Text = this.litName.Text = (table.Rows[0]["Name"] != DBNull.Value) ? table.Rows[0]["Name"].ToString() : "";
                    this.litContent.Text = (table.Rows[0]["Content"] != DBNull.Value) ? table.Rows[0]["Content"].ToString() : "";
                    this.litBeginDate.Text = (table.Rows[0]["BeginDate"] != DBNull.Value) ? Convert.ToDateTime(table.Rows[0]["BeginDate"].ToString()).ToString("yyyy-MM-dd") : "";
                    this.litEndDate.Text = (table.Rows[0]["EndDate"] != DBNull.Value) ? Convert.ToDateTime(table.Rows[0]["EndDate"].ToString()).ToString("yyyy-MM-dd") : "";
                    this.litTypeName.Text = (table.Rows[0]["TypeName"] != DBNull.Value) ? table.Rows[0]["TypeName"].ToString() : "";
                    this.litToName.Text = (table.Rows[0]["ToName"] != DBNull.Value) ? table.Rows[0]["ToName"].ToString() : "";
                    if (table.Rows[0]["Participator"] != DBNull.Value)
                    {
                        this.litParticipatorName.Text = StringHelper.GetUserNames(base.Conn, table.Rows[0]["Participator"].ToString());
                    }
                    if (table.Rows[0]["Manager"] != DBNull.Value)
                    {
                        this.litManagerName.Text = StringHelper.GetUserNames(base.Conn, table.Rows[0]["Manager"].ToString());
                    }
                    this.litCreatorName.Text = "<U title=\"部门：" + table.Rows[0]["DeptName"].ToString() + "\" style=\"CURSOR: hand\">" + table.Rows[0]["CreatorName"].ToString() + "</U>";
                    this.litCreateDate.Text = (table.Rows[0]["CreateDate"] != DBNull.Value) ? Convert.ToDateTime(table.Rows[0]["CreateDate"].ToString()).ToString("yyyy-MM-dd") : "";
                    if (table.Rows[0]["AttachmentId"] != DBNull.Value)
                    {
                        string attachmentId = table.Rows[0]["AttachmentId"].ToString();
                        if ((this.litAttachment.Text = AttachmentBiz.Display(base.Conn, attachmentId)) == string.Empty)
                        {
                            this.litAttachment.Text = "无";
                        }
                    }
                    this.litAttachmentComment.Text = ((table.Rows[0]["AttachmentComment"] != DBNull.Value) ? table.Rows[0]["AttachmentComment"].ToString() : "").Replace("\r\n", "<br>");
                    this.litRemark.Text = ((table.Rows[0]["Remark"] != DBNull.Value) ? table.Rows[0]["Remark"].ToString() : "").Replace("\r\n", "<br>");
                    this.litStatus.Text = this.GetStatusDesc(Convert.ToInt32(table.Rows[0]["Status"].ToString()));
                    this.ViewState["CompletedPercent"] = Convert.ToInt32(table.Rows[0]["CompletedPercent"].ToString());
                }
            }
        }
 

    }
}
